using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using Castle.ActiveRecord.Framework.Config;
using WOODIFramework.Model;

namespace WOODIFramework.UI.Test
{
    /// <summary>
    ///This is a test class for WOODIFramework.Model.Page and is intended
    ///to contain all WOODIFramework.Model.Page Unit Tests
    ///</summary>
    [TestClass()]
    class BuildPages
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            InitActiveRecord();
            RecreateSchema();
        }
        //
        //Use TestCleanup to run code after each test has run
        //
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        

        private void InitActiveRecord()
        {
            XmlConfigurationSource config = new XmlConfigurationSource("NonTestConfig.xml");
            ActiveRecordStarter.Initialize(new System.Reflection.Assembly[] { System.Reflection.Assembly.Load("WOODIFramework.Model"), System.Reflection.Assembly.Load("WOODIFramework.UI") }, config);
            
        }

        private void RecreateSchema()
        {
            ActiveRecordStarter.DropSchema();
            ActiveRecordStarter.CreateSchema();
        }

        [TestMethod()]
        public void BuildAllPages()
        {
            BuildTestBlogs();
            BuildViewBlogPage();
            BuildIndexPage();
            BuildViewBlogsByCategoryPage();
        }

        [TestMethod]
        public void BuildTestBlogs()
        {
            Model.Category woodiFramework = Model.Category.CreateCategory("WOODIFramework");
            woodiFramework.Description = "Blogs about this";
            woodiFramework.Create();

            Model.Category castleProject = Model.Category.CreateCategory("Castle Project");
            castleProject.Description = "Blogs about the Castle Project, including MonoRail, Active Record and Windsor";
            castleProject.Create();

            Model.Category ruby = Model.Category.CreateCategory("Ruby");
            ruby.Description = "Blogs about Ruby";
            ruby.Create();

            Model.Blog firstBlog = Model.Blog.CreateBlog("My first blog");
            firstBlog.SubTitle = "Welcome to the WOODIFramework blog";
            firstBlog.BlogCategory = woodiFramework;
            firstBlog.Body = "This is a test blog to see what happens with this.";
            firstBlog.DateAdded = DateTime.Now;
            firstBlog.Create();
        }

        [TestMethod()]
        public void BuildViewBlogPage()
        {
            Page page = BasicPage.NewBasicPage("ViewBlog");
            ActiveRecordObject requiredObject = new ActiveRecordObject();
            requiredObject.ObjectType = typeof(Model.Blog);

            page.RequiredObjects.Add("Blog", requiredObject);
            page.RequiredObjectsProperties.Add("Blog", new string[] { "BlogId" });
            page.Create();

            Label titleLabel = Label.CreateLabel("TitleLabel");
            titleLabel.DataBoundObjectProperties.Add("Blog", "Title");
            titleLabel.HeaderLevel = 1;
            titleLabel.Create();
            page.Items.Add(titleLabel);

            Label subTitleLabel = Label.CreateLabel("SubTitleLabel");
            subTitleLabel.DataBoundObjectProperties.Add("Blog", "SubTitle");
            subTitleLabel.HeaderLevel = 2;
            subTitleLabel.Create();
            page.Items.Add(subTitleLabel);

            Label bodyLabel = Label.CreateLabel("BodyLabel");
            bodyLabel.DataBoundObjectProperties.Add("Blog", "SubTitle");
            bodyLabel.Create();
            page.Items.Add(bodyLabel);

            page.Update();

        }
        
        public void BuildIndexPage()
        {
            Page page = BasicPage.NewBasicPage("Index");
            ActiveRecordObject[] requiredObject = new ActiveRecordObject[] { };
            //requiredObject.ar = typeof(Model.Blog[]);
            //requiredObject.PrimaryKeyValue = businessUnit.Id.ToString();
            page.RequiredCollections.Add("Blog", requiredObject);
            page.RequiredObjectsMethod.Add("Blog", "FindAll");
            page.BoundCollectionsType.Add("Blog", typeof(Model.Blog));
            page.Create();


            BasicGrid grid = BasicGrid.CreateBasicGrid("BlogGrid");
            grid.BoundObjectName = "Blog";
            grid.CollectionName = "Blogs";
            grid.ShowHeader = false;
            grid.Create();

            BasicGridColumn titleColumn = BasicGridColumn.CreateBasicGridColumn("Title", "Title");
            titleColumn.Display = true;
            titleColumn.PageItemOrder = 1;
            titleColumn.IsBold = true;
            titleColumn.DataBoundObjectProperties.Add("Blog", "Id");
            titleColumn.HyperlinkFormatText = "ViewBlog.rails?BlogId={0}";
            titleColumn.IsHyperlink = true;
            titleColumn.Create();
            grid.Columns.Add(titleColumn);

            BasicGridColumn subTitleColumn = BasicGridColumn.CreateBasicGridColumn("SubTitle", "SubTitle");
            subTitleColumn.Display = true;
            subTitleColumn.PageItemOrder = 2;
            subTitleColumn.Create();
            grid.Columns.Add(subTitleColumn);

            grid.Update();

            page.Items.Add(grid);
            page.Update();
        }

        public void BuildViewBlogsByCategoryPage()
        {
            Page page = BasicPage.NewBasicPage("ViewBlogsByCategory");
            ActiveRecordObject[] requiredObject = new ActiveRecordObject[] { };
            //requiredObject.ar = typeof(Model.Blog[]);
            //requiredObject.PrimaryKeyValue = businessUnit.Id.ToString();
            page.RequiredCollections.Add("Blog", requiredObject);
            page.RequiredObjectsMethod.Add("Blog", "FindBlogsByCategory");
            page.RequiredObjectsProperties.Add("Blog", new string[] { "CategoryId" });
            page.BoundCollectionsType.Add("Blog", typeof(Model.Blog));
            page.Create();


            BasicGrid grid = BasicGrid.CreateBasicGrid("BlogGrid");
            grid.BoundObjectName = "Blog";
            grid.CollectionName = "Blogs";
            grid.ShowHeader = false;
            grid.Create();

            BasicGridColumn titleColumn = BasicGridColumn.CreateBasicGridColumn("Title", "Title");
            titleColumn.Display = true;
            titleColumn.PageItemOrder = 1;
            titleColumn.IsBold = true;
            titleColumn.DataBoundObjectProperties.Add("Blog", "Id");
            titleColumn.HyperlinkFormatText = "ViewBlog.rails?BlogId={0}";
            titleColumn.IsHyperlink = true;
            titleColumn.Create();
            grid.Columns.Add(titleColumn);

            BasicGridColumn subTitleColumn = BasicGridColumn.CreateBasicGridColumn("SubTitle", "SubTitle");
            subTitleColumn.Display = true;
            subTitleColumn.PageItemOrder = 2;
            subTitleColumn.Create();
            grid.Columns.Add(subTitleColumn);

            grid.Update();

            page.Items.Add(grid);
            page.Update();
        }

        [TestMethod()]
        public void BuildBasicPage()
        {
            Page page = BasicPage.NewBasicPage("BasicPage");
            Label label = Label.CreateLabel("Title", "Basic Page");
            label.Tooltip = "Test tooltip";
            label.Create();
            page.Items.Add(label);

            page.Create();
        }

        [TestMethod()]
        public void BuildFormPage()
        {
            FormPage page = (FormPage)FormPage.NewFormPage("FormPage");
            page.Action = "ProcessFormPage.rails";
            #region Title Label
            Label label = Label.CreateLabel("Title", "Form Page");
            label.Tooltip = "This is the page title";
            label.Create();
            page.Items.Add(label);
            #endregion

            #region Name Label
            label = Label.CreateLabel("NameLabel", "Name:");
            label.Tooltip = "Name of item";
            label.Create();
            page.Items.Add(label);
            #endregion

            #region Name Textbox
            TextBox textBox = TextBox.CreateTextBox("NameTextBox", "Kevin McDonnell");
            textBox.Tooltip = "Name of the person";
            textBox.Create();
            page.Items.Add(textBox);
            #endregion

            #region Submit Button
            SubmitButton submitButton = SubmitButton.CreateSubmitButton("SubmitButton", "Submit");
            submitButton.Tooltip = "Submit the form";
            submitButton.Create();
            page.Items.Add(submitButton);
            #endregion
            page.Create();
        }
    }
}
